package com.luxuewen.sxa.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.luxuewen.sxa.common.enums.ResponseEnum;
import com.luxuewen.sxa.common.exception.ApiException;
import com.luxuewen.sxa.entity.*;
import com.luxuewen.sxa.entity.validator.SetPermissionVo;
import com.luxuewen.sxa.service.SxaRolesService;
import com.luxuewen.sxa.mapper.SxaRolesMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author wen
* @description 针对表【sxa_roles】的数据库操作Service实现
* @createDate 2023-11-01 16:25:36
*/
@Service
public class SxaRolesServiceImpl extends ServiceImpl<SxaRolesMapper, SxaRoles>
    implements SxaRolesService{
    @Autowired
    private SxaRoleApisServiceImpl sxaRoleApisService;

    @Autowired
    private SxaApisServiceImpl sxaApisService;

    @Autowired
    private SxaRoleMenusServiceImpl sxaRoleMenusService;

    @Transactional(rollbackFor = Exception.class)
    public Boolean setPermission (SetPermissionVo setPermissionVo) {
        Integer roleId = setPermissionVo.getId();

        // 菜单/按钮权限初始化
        QueryWrapper<SxaRoleMenus> sxaRoleMenusQueryWrapper = new QueryWrapper<>();
        sxaRoleMenusQueryWrapper.eq("role_id", roleId);
        boolean ex = sxaRoleMenusService.exists(sxaRoleMenusQueryWrapper);
        if (ex) {
            boolean removeRes = sxaRoleMenusService.remove(sxaRoleMenusQueryWrapper);
            if (!removeRes) {
                throw new ApiException(ResponseEnum.FAILURE.getCode(), "角色菜单权限初始化失败！");
            }
        }

        QueryWrapper<SxaRoleApis> sxaRoleApisQueryWrapper = new QueryWrapper<>();
        sxaRoleApisQueryWrapper.eq("role_id", roleId);
        boolean roleApiEx = sxaRoleApisService.exists(sxaRoleApisQueryWrapper);
        if (roleApiEx) {
            boolean roleApiRes = sxaRoleApisService.remove(sxaRoleApisQueryWrapper);
            if (!roleApiRes) {
                throw new ApiException(ResponseEnum.FAILURE.getCode(), "角色接口权限初始化失败！");
            }
        }

        List<SxaRoleMenus> roleMenusList = new ArrayList<>();
        List<SxaRoleApis> roleApisList = new ArrayList<>();
        for (Integer menuId : setPermissionVo.getMenuIds()) {
            // 角色菜单关系
            SxaRoleMenus sxaRoleMenus = new SxaRoleMenus();
            sxaRoleMenus.setRoleId(roleId);
            sxaRoleMenus.setMenuId(menuId);
            roleMenusList.add(sxaRoleMenus);

            // 获取菜单所属接口
            QueryWrapper<SxaApis> sxaApisQueryWrapper = new QueryWrapper<>();
            sxaApisQueryWrapper.eq("menu_id", menuId).select("id");
            List<SxaApis> sxaApisList = sxaApisService.list(sxaApisQueryWrapper);
            for (SxaApis sxaApi: sxaApisList) {
                SxaRoleApis sxaRoleApis = new SxaRoleApis();
                sxaRoleApis.setApiId(sxaApi.getId());
                sxaRoleApis.setRoleId(roleId);
                roleApisList.add(sxaRoleApis);
            }
        }

        boolean sxaRoleMenuRes = sxaRoleMenusService.saveBatch(roleMenusList);
        if (!sxaRoleMenuRes) {
            throw new ApiException(ResponseEnum.FAILURE.getCode(), "角色菜单权限保存失败！");
        }

        if (!roleApisList.isEmpty()) {
            boolean sxaRoleApiRes = sxaRoleApisService.saveBatch(roleApisList);
            if (!sxaRoleApiRes) {
                throw new ApiException(ResponseEnum.FAILURE.getCode(), "角色接口权限保存失败！");
            }
        }

        return true;
    }

}




